//timer.h

#include <pthread.h>

class CTimer;

class CTimerCallbackInterface
{
public:
	virtual void onTimer(const CTimer* timer) = 0;
};

class CTimer 
{
public:		
	
	typedef enum
	{
		TIMER_STOP	= 0,
		TIMER_RUN,
		TIMER_PAUSE 
	} TimerState;
	
	CTimer(CTimerCallbackInterface* callback,unsigned long interval,void* userData,bool repeat = true);
	
	void resume();
	void pause();
	void stop();	// calling this will destroy CTimer object
	
	static void msleep(unsigned int ms);
	
	unsigned long interval() const				{ return m_ulInterval;	}
	void* userData() const						{ return m_pUserData;	}
	bool isRepeat() const						{ return m_bRepeat;		}
	CTimerCallbackInterface* callback() const	{ return m_pCallback;	}
	TimerState state() const					{ return m_state;		}
	~CTimer();
    CTimer(){};
private:
	CTimerCallbackInterface*	m_pCallback;
	void*						m_pUserData;
	bool						m_bRepeat;
	unsigned long				m_ulInterval;
	pthread_t					m_thread;
	TimerState					m_state;
	
	friend void* onRun(void* ptr);
	
	void start();
	
	
	
};

class CTest : public CTimerCallbackInterface
{
public:
    void startTest();
    
    void onTimer(const CTimer* timer);
    
private:
    CTimer* m_pTimer;
};
